<?php
/**
 * User: 五哥派
 * Date: 2024/6/30
 * Tel(同WX): 17335592719
 */
namespace app\admin\controller\store;

use app\admin\controller\Base;
use app\admin\model\store\Specs as SpecsModel;
use app\admin\model\store\SpecsValue as SpecsValueModel;
use http\Exception;
use think\Db;

class Specs extends Base{

    protected $model;
    protected $mSpecsValueModel;

    public function __construct(SpecsModel $model, SpecsValueModel $mSpecsValueModel){
        parent::__construct();
        $this->model = $model;
        $this->mSpecsValueModel = $mSpecsValueModel;
    }

    public function lst(){
        return $this->fetch();
    }
    public function lstajax(){
        $where['status'] = 1;
        $list = $this->model->getList($where);
        $result = $list->toArray();
        return showLayui($result['total'], $result['data']);
    }

    public function add(){
        if(request()->isPost()){
            $data = request()->param();
            //{"name":"颜色1","specs_name[0]":"白色","specs_name[1]":"红色","specs_name[2]":"紫色","specs_name[3]":"绿色","specs_name[4]":"黑色"}
            $res = $this->model->saveData($data,false);
            if($res){
                $id = $this->model->id;
                $specsValuesData = [];
                foreach ($data['specs_name'] as $k=>$v) {
                    $specsValuesData[$k]['specs_id'] = $id;
                    $specsValuesData[$k]['name'] = $v;
                    $specsValuesData[$k]['status'] = 1;
                }

                $this->mSpecsValueModel->saveAll($specsValuesData);//新增    更新$user->saveAll($list, false);
                return success('保存成功!');
            }else{
               return error('保存失败!');
            }
        }

        return $this->fetch();
    }
    public function edit(){
        if(request()->isPost()){
            $data = request()->param();
            //dump($data);die;
            $res = $this->model->saveData($data,true);
            if($res){
                $id = $data['id'];//属性id
                $specsValuesData = [];
                //编辑的编辑 新增的新增
                foreach ($data['specs_name'] as $k=>$v) {
                    if($data['specs_value_id'][$k] == 0){
                        $a[$k]['specs_name'] = $data['specs_name'][$k];
                        $a[$k]['specs_value_id'] = $data['specs_value_id'][$k];
                        $specsValuesDataTmp = [];
                        $specsValuesDataTmp['specs_id'] = $id;
                        $specsValuesDataTmp['name'] = $v;
                        $specsValuesDataTmp['status'] = 1;
                        $this->mSpecsValueModel->saveData($specsValuesDataTmp, false);//新增
                    }else{
                        $specsValuesDataTmp = [];
                        //$specsValuesDataTmp['specs_id'] = $id;
                        $specsValuesDataTmp['name'] = $v;
                        $this->mSpecsValueModel->updateById($data['specs_value_id'][$k], $specsValuesDataTmp);
                        $b[$k]['specs_name'] = $data['specs_name'][$k];
                        $b[$k]['specs_value_id'] = $data['specs_value_id'][$k];
                    }

                }
//dump($a);
                //dump($b);
                //die;
                return success('保存成功!');
            }else{
                return error('保存失败!');
            }
        }
        $id = input('id');
        $data = $this->model->findDataByID($id);
        $this->assign('data',$data);

        $specs_values_list = $this->mSpecsValueModel->getList(['specs_id' => $id]);
        $result = $specs_values_list->toArray();
        $this->assign('specs_values_list',$result);

        return $this->fetch();
    }

    public function del(){
        $id = input('id');
        Db::startTrans();
        try{
            //删除前查看商品在是否使用，使用则不能删除
            //同时删除 属性值
            $where['status'] = 1;
            $where['specs_id'] = $id;
            $field = '*';
            $data = $this->mSpecsValueModel->queryData($where, $field);
            if(count($data) > 0){
                $list = [];
                foreach ($data as $k => $item){
                    $list[$k] = $item['id'];
                }
                //$ids = implode(',', $list);
                //$where2[] = ['id', 'in', $ids];
                $ress = $this->mSpecsValueModel->delDataByWhere($list);
            }

            $res = $this->model->delDataByID($id);

            Db::commit();
            return success('删除成功!');
        }catch (Exception $ex){
            Db::rollback();
            //Log::record($ex, 'payerror');
            return error('删除失败!');
        }

    }

    public function dialog() {
        $list = $this->model->getList();
        $result = $list->toArray();
        $this->assign('list',$result);
        return $this->fetch();
       /* return view("", [
            "specs" => json_encode(config("specs"))
        ]);*/
    }


}




